package ua.at.tsvetkov.samizdat.adapters;

import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.database.ContentProviderSamizdat;
import android.app.Fragment;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AdtGenres extends BaseAdapter /*implements Filterable*/ {

	private Fragment				fragment;
//	private ArrayList<String>	originalData	= null;
//	private ArrayList<String>	currentData		= null;
	private Cursor					cursor;
	private Integer				selectedRow		= -1;
	private View					selectedView;

//	private GenresFilter			filter;

	public AdtGenres(Fragment fragment) {
		this.fragment = fragment;
//		this.currentData = new ArrayList<String>();
		getData();
	}

	@Override
	public int getCount() {
		// return currentData.size();
		if (cursor != null) {
			return cursor.getCount();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// return currentData.get(position);
		cursor.moveToPosition(position);
		return cursor;
	}

	@Override
	public long getItemId(int position) {
		cursor.moveToPosition(position);
		return cursor.getInt(0);
	}

	static class ViewHolder {
		TextView	txtTitle;
		TextView	booksCount;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder item = null;
		cursor.moveToPosition(position);

		if (row == null) {
			LayoutInflater inflater = fragment.getActivity().getLayoutInflater();
			row = inflater.inflate(R.layout.frg_genres_list_view_item, parent, false);

			item = new ViewHolder();
			item.txtTitle = (TextView) row.findViewById(R.id.frg_gernres_list_view_item_genre);
			item.booksCount = (TextView) row.findViewById(R.id.frg_gernres_list_view_item_book_count);

			row.setTag(item);
		} else {
			item = (ViewHolder) row.getTag();
		}
		item.txtTitle.setText(cursor.getString(1));
		item.booksCount.setText(fragment.getActivity().getString(R.string.BOOK_COUNT_IN_CURRENT_GENRE) + " " + cursor.getString(3));


		if (position == selectedRow) {
			row.setBackgroundResource(R.drawable.fragment2_item_bg_gradient_selected);
			this.selectedView = row;

		} else {
			row.setBackgroundResource(R.drawable.fragment2_item_bg_gradient);
		}
		return row;
	}

//	@Override
//	public android.widget.Filter getFilter() {
//		if (filter == null)
//			filter = new GenresFilter();
//		return filter;
//	}

	private void getData() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Cursor cursor = fragment.getActivity().getContentResolver().query(Uri.parse(ContentProviderSamizdat.GENRE_CONTENT_URI + "/"), null, null, null, null);
				cursor.moveToFirst();
				AdtGenres.this.cursor = cursor;
				fragment.getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						notifyDataSetChanged();
					}
				});
			}
		}).start();
	}

//	private class GenresFilter extends Filter {
//
//		@Override
//		protected FilterResults performFiltering(CharSequence constraint) {
//			// NOTE: this function is *always* called from a background thread, and
//			// not the UI thread.
//			constraint = constraint.toString().toLowerCase();
//			FilterResults result = new FilterResults();
//			ArrayList<String> fit = new ArrayList<String>();
//			if (constraint != null && constraint.toString().length() > 0) {
//
//				for (String currentGenre : originalData) {
//					if (currentGenre.toLowerCase().contains(constraint)) {
//						fit.add(currentGenre);
//					}
//				}
//				result.count = fit.size();
//				result.values = fit;
//			} else {
//				result.count = originalData.size();
//				result.values = originalData;
//			}
//			return result;
//		}
//
//		@SuppressWarnings("unchecked")
//		@Override
//		protected void publishResults(CharSequence constraint, FilterResults results) {
//			// NOTE: this function is *always* called from the UI thread.
//			currentData.clear();
//			currentData.addAll((ArrayList<String>) results.values);
//			notifyDataSetChanged();
//		}
//	}

	public void setSelectedRow(Integer selectedRow) {
		this.selectedRow = selectedRow;
	}

	public Integer getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedView(View selectedView) {
		this.selectedView = selectedView;
	}

	public View getSelectedView() {
		return selectedView;
	}

}
